const inquirer = require('inquirer');
const { getCategories, getAlbumDetails, downloadAudio } = require('./api');

const main = async () => {
  try {
    // 获取分类列表
    const categories = await getCategories();
    const categoryChoices = categories.data.data.map(category => ({
      name: category.title,
      value: category.fieldData.albumID
    }));

    // 选择分类
    const { selectedCategory } = await inquirer.prompt([
      {
        type: 'list',
        name: 'selectedCategory',
        message: '请选择一个分类:',
        choices: categoryChoices
      }
    ]);

    console.log('Selected Category:', selectedCategory);

    // 获取专辑详情
    const albumDetails = await getAlbumDetails(selectedCategory);
    console.log('Album Details:', albumDetails);

    // 自动下载所有音频
    for (const audio of albumDetails.data.albumAudioInfoList) {
      console.log(`Downloading audio: ${audio.audioName}`);
      await downloadAudio(audio.audioID, selectedCategory,albumDetails.data.albumInfo.albumName);
      console.log(`Downloaded audio: ${audio.audioName}`);
    }

    console.log('所有音频下载完成！');
  } catch (error) {
    console.error('操作失败:', error);
  }
};

main();